package com.ynet.middleground.mobile.approve.controller;

import com.google.common.base.Throwables;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.mobile.approve.commom.ResponseResult;
import com.ynet.middleground.mobile.approve.constant.ErrorMessageConstant;
import com.ynet.middleground.mobile.approve.request.RecallBackTaskWithReasonReq;
import com.ynet.middleground.mobile.approve.request.RejectTaskReq;
import com.ynet.middleground.mobile.approve.service.ApproveCenterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @ClassName RejectPageController
 * @Description 移动驳回页调用API Controller
 * @Author liwq
 * @Date 2021/3/23 21:31
 * @Version 1.0
 */
@RestController
@RequestMapping("/")
@Api(value = "驳回相关服务", tags = "驳回相关服务")
public class RejectPageController {

    @Autowired
    ApproveCenterService approveCenterService;

    /**
     * 驳回-提交 （仅仅是往审批记录表插入一条记录，并且向渠道发送消息，待渠道端响应消息，移动审批再执行真正的审批操作）
     *
     * @param req
     * @return
     */
    @PostMapping("/task/reject")
    @ApiOperation(value = "驳回提交服务", notes = "驳回提交服务")
    public ResponseResult<Object> reject(@RequestBody @Valid RejectTaskReq req) {
        ResponseResult<Object> responseResult = new ResponseResult<>();
        try {
            //update by liwa on 2023-03-14 start
            //参数增加驳回原因、附件列表
//            RecallBackTaskReq recallBackTaskReq = new RecallBackTaskReq();
//            recallBackTaskReq.setUserId(req.getUserId());
//            recallBackTaskReq.setTaskId(req.getTaskId());
//            recallBackTaskReq.setComment(req.getComment());
//            recallBackTaskReq.setProcessInstanceId(req.getProcessInstanceId());
//            approveCenterService.rejectToStartUserTask(recallBackTaskReq);

            RecallBackTaskWithReasonReq recallBackTaskWithReasonReq = new RecallBackTaskWithReasonReq();
            recallBackTaskWithReasonReq.setUserId(req.getUserId());
            recallBackTaskWithReasonReq.setTaskId(req.getTaskId());
            recallBackTaskWithReasonReq.setComment(req.getComment());
            recallBackTaskWithReasonReq.setProcessInstanceId(req.getProcessInstanceId());
            recallBackTaskWithReasonReq.setType(req.getType());
            recallBackTaskWithReasonReq.setCommentFiles(req.getCommentFiles());
            approveCenterService.rejectToStartUserTask(recallBackTaskWithReasonReq);
            //update by liwa on 2023-03-14 end
        } catch (BusinessException e) {
            responseResult.setError(e.getCode(), e.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "驳回-提交服务异常, case: {}",
                    Throwables.getStackTraceAsString(e));
            responseResult.setError(ErrorMessageConstant.ECUC0004_CODE, ErrorMessageConstant.ECUC0004_MESSAGE);
        }
        return responseResult;
    }

}
